﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.BO;
using DAL;
using Challenger = BLL.BO.Challenger;

namespace BLL
{
    public class FilmLogic : LogicBase
    {
        public FilmLogic(string connectionString) : base(connectionString)
        {
        }

        public List<BLL.BO.Film> GetAllFilm()
        {
            using (var context = new ZsKategDataContext(connectionString))
            {
                List<BO.Film> ret = new List<BO.Film>();

                foreach (var film in context.Films)
                {
                    var newfilm = new BO.Film
                                         {
                                             Id = film.ID,
                                             Title = film.Title,
                                             Length = film.Length,
                                             IMDB = film.IMDB,
                                             Challengers = new List<Challenger>()
                                         };
                    foreach (var challenger in film.Megnezves.Select(m => m.Challenger))
                    {
                        newfilm.Challengers.Add(new BO.Challenger
                        {
                            Id = challenger.ID,
                            Name = challenger.Name
                        });
                    }

                    ret.Add(newfilm);
                }

                return ret;
            }
        }

        public BO.Film GetFilm(int id)
        {
            using(var context = new ZsKategDataContext(connectionString))
            {
                var film = context.Films.Single(f => f.ID == id);
                
                if(film != null)
                {
                    var ret = new BO.Film
                                  {
                                      Id = film.ID,
                                      Title = film.Title,
                                      Length = film.Length,
                                      IMDB = film.IMDB,
                                      Challengers = new List<Challenger>()
                                  };

                    foreach (var challenger in film.Megnezves.Select(m => m.Challenger))
                    {
                        ret.Challengers.Add(new BO.Challenger()
                        {
                            Id = challenger.ID,
                            Name = challenger.Name
                        });
                    }

                    return ret;
                }

                return null;
            }
        }
    }
}
